<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>05-表格表头固定</title>
</head>

<body>
    <div class="container">
        <table>
            <thead>
                <tr class="red">
                    <th>Name</th>
                    <th>Age</th>
                    <th>Job</th>
                    <th>Color</th>
                    <th>URL</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <!-- ... -->
                <tr class="green">
                    <th>1-Name</th>
                    <th>Age</th>
                    <th>Job</th>
                    <th>Color</th>
                    <th>URL</th>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr class="red">
                    <th>2-Name</th>
                    <th>Age</th>
                    <th>Job</th>
                    <th>Color</th>
                    <th>URL</th>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr class="blue">
                    <th>3-Name</th>
                    <th>Age</th>
                    <th>Job</th>
                    <th>Color</th>
                    <th>URL</th>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <tr>
                    <td>Lorem.</td>
                    <td>Ullam.</td>
                    <td>Vel.</td>
                    <td>At.</td>
                    <td>Quis.</td>
                </tr>
                <!-- ... -->
            </tbody>
        </table>
    </div>
    <style>
        .container {
            height: 200px;
            width: fit-content;
            margin: 40px auto;
            overflow: auto;
        }

        table {
            text-align: left;
            position: relative;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 0.25rem;
        }

        tr:nth-child(even) {
            background: #EFEFEF;
        }

        tr.red th {
            background: #dd4a63;
            color: white;
        }

        tr.green th {
            background: #03C03C;
            color: white;
        }

        tr.blue th {
            background: #1580d8;
            color: white;
        }

        th {
            background: white;
            position: sticky;
            top: 0;
        }
    </style>
</body>

</html>